package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.google.gson.reflect.TypeToken;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyDayBean;
import flc.ast.databinding.ActivityAddCommemorateBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

public class AddCommemorateActivity extends BaseAc<ActivityAddCommemorateBinding> {

    //添加事件
    public static MyDayBean myBean = null;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
    private String selPath = "";

    @Override
    protected int onCreate() {
        return R.layout.activity_add_commemorate;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivAddCommemorateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvComplete.setOnClickListener(this);
        mDataBinding.ivImg.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvComplete://完成
                if (myBean == null) {
                    saveCommemorate();
                } else {
                    updateCommemorate();
                }
                break;
            case R.id.ivImg://添加图片
                //添加背景
                Intent intent = new Intent(this, AddPicActivity.class);
                startActivityForResult(intent, Content.COMMEMORATE_ADD_PICTURE);
                break;
        }
    }

    @Override
    protected void initData() {
        //日前相关
        setDateView();
        String date = "";
        if (myBean == null) {
            date = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd"));
        } else {
            mDataBinding.etName.setText(myBean.getTitle());
            mDataBinding.etThoughts.setText(myBean.getContent());
            long millis = TimeUtils.string2Millis(myBean.getDate(), new SimpleDateFormat("yyyy-MM-dd"));
            date = TimeUtils.millis2String(millis, new SimpleDateFormat("yyyy-MM-dd"));
            selPath = myBean.getImg();
            Glide.with(mContext).load(selPath).into(mDataBinding.ivImg);
        }
        //滚动到对应日期
        String[] split = date.split("-");
        int year = Integer.parseInt(split[0]);//年
        int moon = Integer.parseInt(split[1]);//月
        int day = Integer.parseInt(split[2]);//日
        mDataBinding.wheelView1.setCurrentItem(year - 1980);
        mDataBinding.wheelView2.setCurrentItem(moon - 1);
        mDataBinding.wheelView3.setCurrentItem(day - 1);
    }

    //设置日前选择布局
    private void setDateView() {
        mDataBinding.wheelView1.setDividerColor(Color.parseColor("#FFFFFF"));
        mDataBinding.wheelView1.setTextColorCenter(Color.parseColor("#0095FF"));
        mDataBinding.wheelView1.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView1.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView1.setCurrentItem(43);
        mDataBinding.wheelView1.setItemsVisibleCount(5);
        mDataBinding.wheelView1.setCyclic(true);
        mDataBinding.wheelView2.setDividerColor(Color.parseColor("#FFFFFF"));
        mDataBinding.wheelView2.setTextColorCenter(Color.parseColor("#0095FF"));
        mDataBinding.wheelView2.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView2.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView2.setCurrentItem(0);
        mDataBinding.wheelView2.setItemsVisibleCount(5);
        mDataBinding.wheelView2.setCyclic(true);
        mDataBinding.wheelView3.setDividerColor(Color.parseColor("#FFFFFF"));
        mDataBinding.wheelView3.setTextColorCenter(Color.parseColor("#0095FF"));
        mDataBinding.wheelView3.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView3.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView3.setCurrentItem(0);
        mDataBinding.wheelView3.setItemsVisibleCount(5);
        mDataBinding.wheelView3.setCyclic(true);
        //设置滚轮数据
        List<String> listYear = new ArrayList<>();//年 1980-2040
        for (int i = 0; i < 61; i++) {
            listYear.add(String.valueOf(1980 + i));
        }
        List<String> listMonth = new ArrayList<>();//月 12
        for (int i = 0; i < 12; i++) {
            listMonth.add(String.valueOf((i + 1) < 10 ? "0" + (i + 1) : i + 1));
        }
        WheelAdapter adapter1 = new ArrayWheelAdapter(listYear);
        mDataBinding.wheelView1.setAdapter(adapter1);
        WheelAdapter adapter2 = new ArrayWheelAdapter(listMonth);
        mDataBinding.wheelView2.setAdapter(adapter2);
        setDayDate(31);//日 默认显示1月 共31天
        mDataBinding.wheelView1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {

            }
        });
        //监听月份滚动选择
        mDataBinding.wheelView2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                index = index + 1;
                if (index == 1 || index == 3 || index == 5 || index == 7 || index == 8 || index == 10 || index == 12) {
                    //大月31天
                    setDayDate(31);
                } else if (index == 2) {
                    //2月判断是闰(29)还是平(28)
                    if (TimeUtils.isLeapYear(TimeUtils.getNowString(format), format)) {
                        setDayDate(29);
                    } else {
                        setDayDate(28);
                    }
                } else {
                    //小月30天
                    setDayDate(30);
                }
            }
        });
        mDataBinding.wheelView3.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {

            }
        });
    }

    //设置天
    private void setDayDate(int num) {
        List<String> listDay = new ArrayList<>();//日 默认显示1月31天
        for (int i = 0; i < num; i++) {
            listDay.add(String.valueOf((i + 1) < 10 ? "0" + (i + 1) : i + 1));
        }
        WheelAdapter adapter3 = new ArrayWheelAdapter(listDay);
        mDataBinding.wheelView3.setAdapter(adapter3);
    }

    //保存纪念日
    private void saveCommemorate() {
        //标题
        String title = mDataBinding.etName.getText().toString();
        if (TextUtils.isEmpty(title)) {
            ToastUtils.showShort(R.string.please_input_title);
            return;
        }
        //内容
        String content = mDataBinding.etThoughts.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort(R.string.please_input_content_tip);
            return;
        }
        //tup
        if (TextUtils.isEmpty(selPath)) {
            ToastUtils.showShort(R.string.please_add_img);
            return;
        }
        showDialog(getString(R.string.saving));
        //选择的日期
        int year = mDataBinding.wheelView1.getCurrentItem() + 1980;
        int moon = mDataBinding.wheelView2.getCurrentItem() + 1;
        int day = mDataBinding.wheelView3.getCurrentItem() + 1;
        String date = year + "-" + moon + "-" + day;
        //准备数据
        List<MyDayBean> listSave = new ArrayList<>();
        listSave.add(new MyDayBean(TimeUtils.getNowString(), title, content, date, selPath, 0));
        List<MyDayBean> listSp = AppSpUtils.getCommemorateList();
        if (listSp != null && listSp.size() != 0) {
            listSp.addAll(listSave);
            AppSpUtils.savCommemorateList(listSp);
        } else {
            AppSpUtils.savCommemorateList(listSave);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                finish();
            }
        }, 500);
    }

    //修改纪念日
    private void updateCommemorate() {
        //标题
        String title = mDataBinding.etName.getText().toString();
        if (TextUtils.isEmpty(title)) {
            ToastUtils.showShort(R.string.please_input_title);
            return;
        }
        //内容
        String content = mDataBinding.etThoughts.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort(R.string.please_input_content_tip);
            return;
        }
        //背景
        if (TextUtils.isEmpty(selPath)) {
            ToastUtils.showShort(R.string.please_add_img);
            return;
        }
        showDialog(getString(R.string.saving));
        //选择的日期
        int year = mDataBinding.wheelView1.getCurrentItem() + 1980;
        int moon = mDataBinding.wheelView2.getCurrentItem() + 1;
        int day = mDataBinding.wheelView3.getCurrentItem() + 1;
        String date = year + "-" + moon + "-" + day;
        //保存数据
        List<MyDayBean> listSp = AppSpUtils.getCommemorateList();
        if (listSp != null && listSp.size() > 0) {
            for (int i = 0; i < listSp.size(); i++) {
                if (listSp.get(i).getId().equals(myBean.getId())) {
                    listSp.get(i).setTitle(title);
                    listSp.get(i).setContent(content);
                    listSp.get(i).setDate(date);
                    listSp.get(i).setImg(selPath);
                    break;
                }
            }
            AppSpUtils.savCommemorateList(listSp);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                finish();
            }
        }, 500);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == Content.COMMEMORATE_ADD_PICTURE) {
            selPath = data.getStringExtra(Content.PICTURE_PATH);
            Glide.with(mContext).load(selPath).into(mDataBinding.ivImg);
        }
    }

}